// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/services/file_util/xz_file_extractor.h"

#include <stdint.h>

#include <string>
#include <utility>

#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "mojo/public/c/system/types.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chrome {

using ::testing::StrictMock;

class XzFileExtractorTest : public testing::Test {
 protected:
  XzFileExtractorTest() = default;
  ~XzFileExtractorTest() override = default;

  base::test::TaskEnvironment task_environment_;
};

TEST_F(XzFileExtractorTest, Extract) {
  // Generated by echo test | xz | xxd --include
  const uint8_t kTestData[] = {
      0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, 0xe6, 0xd6, 0xb4,
      0x46, 0x02, 0x00, 0x21, 0x01, 0x16, 0x00, 0x00, 0x00, 0x74, 0x2f,
      0xe5, 0xa3, 0x01, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74, 0x0a, 0x00,
      0x00, 0x00, 0x00, 0x9d, 0xed, 0x31, 0x1d, 0x0f, 0x9f, 0xd7, 0xe6,
      0x00, 0x01, 0x1d, 0x05, 0xb8, 0x2d, 0x80, 0xaf, 0x1f, 0xb6, 0xf3,
      0x7d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5a};

  mojo::ScopedDataPipeProducerHandle xz_producer;
  mojo::ScopedDataPipeConsumerHandle xz_consumer;
  ASSERT_EQ(MOJO_RESULT_OK,
            mojo::CreateDataPipe(nullptr, xz_producer, xz_consumer));

  mojo::ScopedDataPipeProducerHandle extracted_producer;
  mojo::ScopedDataPipeConsumerHandle extracted_consumer;
  ASSERT_EQ(MOJO_RESULT_OK, mojo::CreateDataPipe(nullptr, extracted_producer,
                                                 extracted_consumer));

  ASSERT_TRUE(mojo::BlockingCopyFromString(
      std::string(kTestData, kTestData + sizeof(kTestData)), xz_producer));
  xz_producer.reset();

  StrictMock<base::MockCallback<XzFileExtractor::ExtractCallback>> callback;
  base::RunLoop run_loop;
  EXPECT_CALL(callback, Run(true))
      .WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
  XzFileExtractor extractor;
  extractor.Extract(std::move(xz_consumer), std::move(extracted_producer),
                    callback.Get());
  run_loop.Run();

  std::string result;
  ASSERT_TRUE(
      mojo::BlockingCopyToString(std::move(extracted_consumer), &result));
  EXPECT_EQ("test\n", result);
}

TEST_F(XzFileExtractorTest, ExtractInvalidData) {
  // Generated by cat /dev/urandom | xxd -l 64 --include
  const uint8_t kTestData[] = {
      0xb9, 0x57, 0xb8, 0xf7, 0x89, 0x06, 0xe5, 0xbc, 0x79, 0x5b, 0x29,
      0xb9, 0x20, 0x63, 0xba, 0xbd, 0x8b, 0x75, 0x6c, 0x61, 0xb7, 0xed,
      0xe8, 0x17, 0x65, 0x06, 0x8d, 0x6f, 0x25, 0x4b, 0x44, 0x7f, 0x07,
      0xe1, 0x19, 0x5a, 0x1d, 0xb5, 0x8a, 0xf2, 0x82, 0xad, 0xb3, 0xa4,
      0x0c, 0x65, 0xbd, 0x8f, 0x69, 0x4c, 0xa6, 0xc3, 0xb9, 0x4c, 0xcd,
      0x87, 0x5e, 0x3a, 0x2a, 0x36, 0x6d, 0xc9, 0xd1, 0x07};

  mojo::ScopedDataPipeProducerHandle xz_producer;
  mojo::ScopedDataPipeConsumerHandle xz_consumer;
  ASSERT_EQ(MOJO_RESULT_OK,
            mojo::CreateDataPipe(nullptr, xz_producer, xz_consumer));

  mojo::ScopedDataPipeProducerHandle extracted_producer;
  mojo::ScopedDataPipeConsumerHandle extracted_consumer;
  ASSERT_EQ(MOJO_RESULT_OK, mojo::CreateDataPipe(nullptr, extracted_producer,
                                                 extracted_consumer));

  ASSERT_TRUE(mojo::BlockingCopyFromString(
      std::string(kTestData, kTestData + sizeof(kTestData)), xz_producer));
  xz_producer.reset();

  StrictMock<base::MockCallback<XzFileExtractor::ExtractCallback>> callback;
  base::RunLoop run_loop;
  EXPECT_CALL(callback, Run(false))
      .WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
  XzFileExtractor extractor;
  extractor.Extract(std::move(xz_consumer), std::move(extracted_producer),
                    callback.Get());
  run_loop.Run();

  std::string result;
  ASSERT_TRUE(
      mojo::BlockingCopyToString(std::move(extracted_consumer), &result));
  EXPECT_TRUE(result.empty());
}

TEST_F(XzFileExtractorTest, ExtractEmptyData) {
  mojo::ScopedDataPipeProducerHandle xz_producer;
  mojo::ScopedDataPipeConsumerHandle xz_consumer;
  ASSERT_EQ(MOJO_RESULT_OK,
            mojo::CreateDataPipe(nullptr, xz_producer, xz_consumer));

  mojo::ScopedDataPipeProducerHandle extracted_producer;
  mojo::ScopedDataPipeConsumerHandle extracted_consumer;
  ASSERT_EQ(MOJO_RESULT_OK, mojo::CreateDataPipe(nullptr, extracted_producer,
                                                 extracted_consumer));
  xz_producer.reset();

  StrictMock<base::MockCallback<XzFileExtractor::ExtractCallback>> callback;
  base::RunLoop run_loop;
  EXPECT_CALL(callback, Run(false))
      .WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
  XzFileExtractor extractor;
  extractor.Extract(std::move(xz_consumer), std::move(extracted_producer),
                    callback.Get());
  run_loop.Run();

  std::string result;
  ASSERT_TRUE(
      mojo::BlockingCopyToString(std::move(extracted_consumer), &result));
  EXPECT_TRUE(result.empty());
}

}  // namespace chrome
